/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utility;

//import com.chilkatsoft.*;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author sh
 */
public class SendMail {

//    static {
//    try {
//        //System.out.println("path:"+System.getProperty("java.library.path"));
//        System.load("D:/iti/java/trunk/testHB/src/java/Utility/chilkat.dll");
////        String libpath = System.getProperty("java.library.path");
////    	libpath = libpath + ";/chilkatJava";
////    	System.setProperty("java.library.path",libpath);
////
////    	System.loadLibrary("chilkat");
//    } catch (UnsatisfiedLinkError e) {
//      System.err.println("Native code library failed to load.\n" + e);
//      System.exit(1);
//    }
//  }

  public  void send(String to,String subject,String body,String from)
  {


      SendMail_New(to, subject, body);
//    CkMailMan mailman = new CkMailMan();
//
//    //  Any string argument automatically begins the 30-day trial.
//    boolean success;
//    success = mailman.UnlockComponent("30-day trial");
//    if (success != true) {
//        System.out.println("Component unlock failed");
//        return;
//    }
//
//    String recipient;
//    recipient = to;
//
//    //  Do a DNS MX lookup for the recipient's mail server.
//    String smtpHostname;
//    smtpHostname = mailman.mxLookup(recipient);
//    if (smtpHostname == null ) {
//        System.out.println(mailman.lastErrorText());
//        return;
//    }
//
//    System.out.println(smtpHostname);
//
//    //  Set the SMTP server.
//    mailman.put_SmtpHost(smtpHostname);
//
//    //  Create a new email object
//    CkEmail email = new CkEmail();
//
//    email.put_Subject(subject);
//    email.put_Body(body);
//    email.put_From(from);
//    email.AddTo("",recipient);
//
//    success = mailman.SendEmail(email);
//    if (success != true) {
//        System.out.println(mailman.lastErrorText());
//    }
//    else {
//        System.out.println("Mail Sent!");
//    }

  }
  public void SendMail_New(String to,String subject,String body)
  {
      
       try {
     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
   final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
   Properties props = System.getProperties();
   props.setProperty("mail.smtp.host", "smtp.gmail.com");
   props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
   props.setProperty("mail.smtp.socketFactory.fallback", "false");
   props.setProperty("mail.smtp.port", "465");
   props.setProperty("mail.smtp.socketFactory.port", "465");
   props.put("mail.smtp.auth", "true");
   final String username = "noreply.jest@gmail.com";
   final String password = "adminjets";
   Session session = Session.getDefaultInstance(props,
     new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(username,
         password);
      }
     });

   // -- Create a new message --
   Message msg = new MimeMessage(session);

   // -- Set the FROM and TO fields --
   msg.setFrom(new InternetAddress("albbb@gmail.com"));
   msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
     to, true));
   msg.setSubject(subject);
   msg.setText(body);
   msg.setSentDate(new Date());
   Transport.send(msg);

  } catch (MessagingException e) {
e.printStackTrace();
     }
      
      
      
      
      
  }
  
  
  
//  public static void main(String[] args){
//      SendMail mail=new SendMail();
//      mail.send("noreply.jest@gmail.com", "test", "send msg", "fol@zay_elfol.com");
//  }


}
